﻿using Quartz;
using Volo.Abp.DependencyInjection;
using Volo.Abp.Uow;

namespace FlyingEye.MonitorService
{
    public class DevicePartRuntimeMonitorJob : IJob, ITransientDependency
    {
        private readonly IServiceScopeFactory _factory;

        public DevicePartRuntimeMonitorJob(IServiceScopeFactory factory)
        {
            _factory = factory;
        }

        public async Task Execute(IJobExecutionContext context)
        {
            using var scope = _factory.CreateScope();
            var service = scope.ServiceProvider;
            var uowManager = service.GetRequiredService<IUnitOfWorkManager>();

            using (var uow = uowManager.Begin(new AbpUnitOfWorkOptions()))
            {
                var monitorService = service.GetRequiredService<DevicePartRuntimeMonitorService>();
                await monitorService.ExecuteMonitorTasksAsync();
                await uow.CompleteAsync();
            }
        }
    }
}
